<template>
  <su-page bg="#f7f7f7">
    <su-body :end="listAllLoaded && listTotal">
      <div class="p10 white-bg">
        <u-search v-model="queryParams.name" placeholder="输入关键词搜索" :showAction="false" @search="filterList"></u-search>
      </div>
      <!-- 筛选 -->
      <su-filter-bar stickyTop="0" padding="0 5px">
        <u-tabs :list="tabList" :current="queryParams.category" keyName="name" @click="changecurrent"></u-tabs>
      </su-filter-bar>
      <!-- list -->
      <view  class="p10" v-if="tabList.values(1)">
        <su-grant-card v-for="(item,index) in list" :key="index" :data="item"></su-grant-card>
      </view>
	 <!-- <view  class="p10" v-if="tabList.values(2)">
	    <su-grant-card v-for="(item,index) in list" :key="index" :data="item"></su-grant-card>
	  </view> -->
      <su-list-loading :loading="listLoading" :listLength="list.length"></su-list-loading>
      <!-- 无数据 -->
      <u-empty v-if="list.length == 0" class="mt40" mode="data"></u-empty>      
      <!-- <u-empty v-if="listAllLoaded && listTotal==0" class="mt40" mode="data"></u-empty>      -->
    </su-body>
  </su-page>
</template>

<script>
	/**
	  * @Description:审核
	  * @author:神仙姐姐
	  * @param 
	  * @return 
	  * @createTime: 2022-11-22 14:50:44
	  * @Copyright by 神仙姐姐
	  */
  import {
    mapState
  } from 'vuex'
  import SuGrantCard from './components/su-grant-card.vue'
  import {runSQL} from '../../../common/utils/wxutils.js'
  export default {
    components: {
      SuGrantCard
    },
    data() {
      return {
		// tpagestart:0,
		// wpagestart:0,
        tabList: [{
            value: '1',
            name: '我发起的'
          },
          {
            value: '2',
            name: '待我审批',
            // badge: {
            //   value: 5,
            // }
          },
        ],
        // 查询参数
        queryParams: {
          wpagestart: 0,
		  tpagestart:0,
          // pageNum: 1,
          pageSize: 5,
          name: null,
          category: 0,
          key: null,
          tenantId: null,
          deployTime: null,
          derivedFrom: null,
          derivedFromRoot: null,
          parentDeploymentId: null,
          engineVersion: null
        },
        searchKey: '',
        list: [],
        listTotal: 0,
        listAllLoaded: false,
        listLoading: false,
      }
    },
    computed: {
      ...mapState(['token'])
    },
    onLoad(params) {
      this.reloadlist({index:0})
    },
    watch: {
      token(newVal) {
        this.reloadlist()
      }
    },
    methods: {
	  changecurrent(current){
		  // console.log('点击了')
		  // console.log(current.index)
		  this.listLoading = false
		  this.listAllLoaded = false
		  this.queryParams.name = '';
		  this.queryParams.wpagestart = 0;
		  this.queryParams.tpagestart = 0;
		  this.queryParams.category = current.index;
		  this.list = []
		  this.getlist()
	  },
	  filterList(){
		  
		  let sthis = this;
		  // debugger;
		  this.list.length = 0;
		  let res = {
		    data:{
		      records:[],
		      // total:5
		    }
		  }
		  if(this.queryParams.category == 0){
			  this.queryParams.wpagestart = 0;
			  runSQL(`select * from ssnydw.FQLC where  (LCBT like '%${this.queryParams.name}%' or JSDW like '%${this.queryParams.name}%' or FSDW like '%${this.queryParams.name}%'
			 or FSR like '%${this.queryParams.name}%' or JSR like '%${this.queryParams.name}%' ) and FSRid = ${JSON.parse(uni.getStorageSync('userInfo')).HUMANID} limit 5 offset ${sthis.queryParams.tpagestart};`).then(wd=>{
				  if(wd[0].length>0){
					  res.data.records = wd[0];
				  }
				  this.queryParams.wpagestart +=5
				  this.dataLoadedHandler(res)
			  })
		  }else{
			 this.queryParams.tpagestart = 0
			 // console.log(`select * from ssnydw.FQLC where  (LCBT like '%${this.queryParams.name}%' or JSDW like '%${this.queryParams.name}%' or FSDW like '%${this.queryParams.name}%'
			 // or FSR like '%${this.queryParams.name}%' or JSR like '%${this.queryParams.name}%' ) and JSRid = ${JSON.parse(uni.getStorageSync('userInfo')).HUMANID} limit 5 offset ${sthis.queryParams.tpagestart};`)
			 // 				  // return
			 runSQL(`select * from ssnydw.FQLC where  (LCBT like '%${this.queryParams.name}%' or JSDW like '%${this.queryParams.name}%' or FSDW like '%${this.queryParams.name}%'
			 or FSR like '%${this.queryParams.name}%' or JSR like '%${this.queryParams.name}%' ) and JSRid like '%${JSON.parse(uni.getStorageSync('userInfo')).HUMANID}%' limit 5 offset ${sthis.queryParams.tpagestart};`).then(db=>{
				 if(db[0].length>0){
						res.data.records = db[0];
				  }
				  
				  this.queryParams.tpagestart +=5
				  this.dataLoadedHandler(res)
			 }) 
		  }
	  },
      //初始化参数，重新加载列表
      reloadlist(stopPullDownRefresh) {
		// this.currentIndex = stopPullDownRefresh
        this.listLoading = false
        this.listAllLoaded = false
        this.list = []
        this.listTotal = 0
        this.queryParams.wpagestart = 0
        this.queryParams.tpagestart = 0
        this.getlist(stopPullDownRefresh)
      },
      //获取列表数据
      getlist(stopPullDownRefresh) {
		let sthis = this;
        if (this.listLoading || this.listAllLoaded) return
        this.listLoading = true
          let res = {
            data:{
              records:[],
              // total:5
            }
          }
		  if(sthis.queryParams.category == 0){
			  runSQL(`select * from ssnydw.FQLC where FSRid = ${JSON.parse(uni.getStorageSync('userInfo')).HUMANID} limit 5 offset ${sthis.queryParams.wpagestart};`).then(wd=>{
				  if(wd[0].length>0){
					  console.log( wd[0])
					  res.data.records = wd[0];
					  this.queryParams.wpagestart +=5
					  this.dataLoadedHandler(res, stopPullDownRefresh)
				  }
				 
			  })
		  }else{
			  console.log(`select * from ssnydw.FQLC where JSRid = '${JSON.parse(uni.getStorageSync('userInfo')).HUMANID}' limit 5 offset ${sthis.queryParams.tpagestart};`)
			 runSQL(`select * from ssnydw.FQLC where JSRid like '%${JSON.parse(uni.getStorageSync('userInfo')).HUMANID}%' limit 5 offset ${sthis.queryParams.tpagestart};`).then(db=>{
				  if(db[0].length>0){
						console.log(1111, db[0])
						res.data.records = db[0];
						this.queryParams.tpagestart +=5
						this.dataLoadedHandler(res, stopPullDownRefresh)
				  }else{
					  this.listLoading = false
				  }
				  
				 
			 }) 
		  }
        
      },
      // 列表请求成功后处理
      dataLoadedHandler(res, stopPullDownRefresh) {
		this.listLoading = false
		const resData = res.data || {}
		const pageData = resData.records || []
		this.list = this.list.concat(pageData)
		this.listTotal = resData.total || 0
		if (pageData.length < this.queryParams.pageSize) {
		  this.listAllLoaded = true
		} else {
		  // this.queryParams.pageNum+=5
		}
		if (stopPullDownRefresh) {
		  uni.stopPullDownRefresh()
		}
      },
    },

    onPullDownRefresh() {
		// debugger;
      this.queryParams.name = ''
      this.reloadlist()
    },
    onReachBottom() {
		// debugger;
      console.log('触底')
      this.getlist()
    }
  }
</script>

<style lang="scss">
</style>
